package com.yjhealth.libs.businessdemo.business;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;

import com.yjhealth.libs.businessdemo.R;
import com.yjhealth.libs.businessdemo.fragment.DemoFragment;
import com.yjhealth.libs.commonlib.base.activity.BaseActivity;

import java.util.ArrayList;

public class DemoViewPagerActivity extends BaseActivity {
    
    ViewPager viewpager;

    MyTabAdapter adapter;
    int position;
    
    @Override
    protected void onRefreshView() {
    }
    
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.businessdemo_activity_demo_viewpager);
        position = getIntent().getIntExtra("position", 0);
        initLayout();
        viewpager = findViewById(R.id.viewpager);
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                yjhealthCoreToolbar.setTitle("tt"+position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        ArrayList<Fragment> fragments = new ArrayList<>();
        DemoFragment caseFragment = DemoFragment.newInstance("f1");
        DemoFragment recipeFragment = DemoFragment.newInstance("f2");
        DemoFragment checkFragment = DemoFragment.newInstance("f3");
        fragments.add(caseFragment);
        fragments.add(recipeFragment);
        fragments.add(checkFragment);

        adapter = new MyTabAdapter(getSupportFragmentManager(), fragments);
        viewpager.setOffscreenPageLimit(fragments.size());
        viewpager.setAdapter(adapter);
        viewpager.setCurrentItem(position);
        
    }

    static class MyTabAdapter extends FragmentPagerAdapter {

        private ArrayList<Fragment> fragments;

        public MyTabAdapter(FragmentManager fm, ArrayList<Fragment> fragments) {
            super(fm);
            this.fragments = fragments;

        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return "title"+position;

        }

        @Override
        public long getItemId(int position) {
            return position;
        }
    }
}
